﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Common
{
    public partial class SLTabCtrl : UserControl
    {
        public int SelectedIndex 
        {
            get
            {
                return m_nSelIndex;
            }
            set
            {
                m_nSelIndex = value;
                UpdateTabControl();
            }
        }

        public List<object> Children
        {
            get
            {
                return childControls;
            }
        }

        List<object> childControls = new List<object>();

        int m_nSelIndex = -1;

        public SLTabCtrl()
        {
            InitializeComponent();
        }

        public void AddTabPage(string szHeader, object ctrl)
        {
            AddHeader(szHeader);
            this.content.Children.Add((UIElement)ctrl);
            childControls.Add(ctrl);
            SelectedIndex = childControls.Count-1;
        }

        void AddHeader(string szHeader)
        {
            Border border = new Border();
            border.Margin = new Thickness(5, 5, 0, 0);
            border.HorizontalAlignment = HorizontalAlignment.Left;
            border.VerticalAlignment = VerticalAlignment.Top;
            border.Height = 22;
            border.BorderBrush = new SolidColorBrush(Colors.LightGray);
            border.BorderThickness = new Thickness(1, 1, 1, 1);
            border.CornerRadius = new CornerRadius(2);
            //border.Style = ((System.Windows.Style)App.Current.Resources["TabHeaderBorder"]);
            border.MouseEnter += new MouseEventHandler(border_MouseEnter);
            border.MouseLeave += new MouseEventHandler(border_MouseLeave);
            border.MouseLeftButtonDown += new MouseButtonEventHandler(border_MouseLeftButtonDown);
          
            TextBlock txtBlock = new TextBlock();
            txtBlock.Margin = new Thickness(2);
            txtBlock.FontSize = 11;
            txtBlock.Foreground = new SolidColorBrush(Colors.LightGray);
            txtBlock.TextAlignment = TextAlignment.Center;
            //txtBlock.Style = (System.Windows.Style)App.Current.Resources["TabHeaderText"];
            txtBlock.Text = szHeader;
            
            border.Child = txtBlock;
            this.HeaderPanel.Children.Add(border);
        }

        void border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            for (int i = 0; i < this.HeaderPanel.Children.Count; i++)
            {
                if (sender == this.HeaderPanel.Children[i])
                {
                    SelectedIndex = i;
                    UpdateTabControl();
                    break;
                }
            }
        }

        void UpdateTabControl()
        {
            for (int i = 0; i<childControls.Count; i++)
            {
                Control ctrl = childControls[i] as Control;
                Border Header = this.HeaderPanel.Children[i] as Border;
                if (i == SelectedIndex)
                {
                    ctrl.Visibility = Visibility.Visible;
                    Header.Background = new SolidColorBrush(Colors.Purple);
                }
                else
                {
                    ctrl.Visibility = Visibility.Collapsed;
                    Header.Background = new SolidColorBrush(Colors.Transparent);
                }
            }
        }

        void border_MouseLeave(object sender, MouseEventArgs e)
        {
            Border CurrentHeader = (Border)this.HeaderPanel.Children[SelectedIndex];
            Border border = sender as Border;
            if(CurrentHeader != border)
                border.Background = new SolidColorBrush(Colors.Transparent);
        }

        void border_MouseEnter(object sender, MouseEventArgs e)
        {
            Border CurrentHeader = (Border)this.HeaderPanel.Children[SelectedIndex];
            Border border = sender as Border;
            if (CurrentHeader != border)
                border.Background = new SolidColorBrush(Colors.Purple);
        }
    }
}
